package com.control.admin;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.dao.UserInfoDAO;
import com.entity.UserInfo;
import com.util.PublicToolCheckParam;
import com.util.PublicToolPageModel;
import com.util.Validate;
import com.entity.PageModel;
import java.util.ArrayList;
import com.entity.PageModel;
/**
 * 系统用户管理
 * @author gxkj-042
 *
 */
public class UpdateUserInfo extends HttpServlet {

	private static final long serialVersionUID = -6101002376282176066L;

	public static void checkOldUserPwd(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		String oldUserPwd = Validate.replaceHtml(request.getParameter("oldUserPwd"));
		HttpSession session = request.getSession();
		String userName = (String) session.getAttribute("userName");
		if (PublicToolCheckParam.checkNullAndEmpty(userName)) {
			UserInfoDAO udao = new UserInfoDAO();
			UserInfo ur = udao.getUserId(userName);
			if (udao.checkOldUserPwd(ur.getId(), oldUserPwd) == false) {
				out.println("�����벻��ȷ!����������!");
			} else {
				out.println("��������ȷ!");
			}
		}
	}
	/**
	 * 跳转修改密码页面
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public static void showUpdatePasswordPage(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("../admin/updatePassword.jsp").forward(request, response);
	}
	/**
	 * 跳转修改用户名页面
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public static void showUpdateUserNamedPage(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("../admin/updateUserName.jsp").forward(request, response);
	}
	/**
	 * 修改用户名
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public static void updateUserName(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String newUserName = Validate.replaceHtml(request.getParameter("userName"));
		HttpSession session = request.getSession();
		String userName = (String) session.getAttribute("userName");
		if (PublicToolCheckParam.checkNullAndEmpty(userName)) {
			UserInfoDAO udao = new UserInfoDAO();
			UserInfo ur = udao.getUserId(userName);
			udao.updateUserName(ur.getId(), newUserName);
			request.setAttribute("url", "../admin/ok.jsp");
			session.setAttribute("userName", newUserName);
			session.setAttribute("msg", "�޸��û����ɹ�");
			request.getRequestDispatcher("../admin/systemManage.jsp").forward(request, response);
		}
	}

	/**
	 *  删除 用户信息
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public static void deleteInfo(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String Id = Validate.replaceHtml(request.getParameter("id"));
		UserInfoDAO udao = new UserInfoDAO();
		udao.deleteUserInfo(Integer.parseInt(Id));
		request.setAttribute("error", "�ɹ�ɾ����");
		request.getRequestDispatcher("../servlet/UpdateUserInfo?method=showManageUserInfoPage").forward(request, response);
	}

	/**
	 *  保存增加用户信息
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public static void insertInfo(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String str_userName = Validate.replaceHtml(request.getParameter("username"));
		String str_userRole = Validate.replaceHtml(request.getParameter("userrole"));
		String str_userPwd = "123456";
		UserInfoDAO udao = new UserInfoDAO();
		udao.addUserInfo(str_userName, str_userPwd, str_userRole);
		request.setAttribute("error", "�ɹ������û����û���ʼ����Ϊ123456 ��");
		request.getRequestDispatcher("../servlet/UpdateUserInfo?method=showManageUserInfoPage").forward(request, response);
	}

	/**
	 *  显示增加用户页面
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public static void showAddUserInfoPage(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("../admin/addUserInfo.jsp").forward(request, response);
	}

	/**
	 * 查询系统用户列表
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public static void showManageUserInfoPage(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String cp = Validate.replaceHtml(request.getParameter("cp"));
		UserInfoDAO udao = new UserInfoDAO();
		int sumCount = udao.getSumUserCount();
		PageModel pm = PublicToolPageModel.getPageInfo(request, sumCount,cp);
		ArrayList<Object> userInfoList = udao.getPageModelOfAllUser(pm);
		request.setAttribute("userInfoList", userInfoList);
		request.getRequestDispatcher("../admin/manageUserInfo.jsp").forward(request, response);
	}

	/**
	 * 编辑用户信息
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public static void edituserInfo(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String submit = Validate.replaceHtml(request.getParameter("submit"));
		String userId = Validate.replaceHtml(request.getParameter("id"));
		if (PublicToolCheckParam.checkNullAndEmpty(submit)) {
			if (submit.equals("1")) {
				String str_userName = Validate.replaceHtml(request.getParameter("username"));
				String str_userRole = Validate.replaceHtml(request.getParameter("userrole"));
				UserInfoDAO udao = new UserInfoDAO();
				udao.updateUserInfo(Integer.parseInt(userId), str_userName,str_userRole);
				request.setAttribute("error", "�ɹ��޸��û� ��");
				request.getRequestDispatcher("../servlet/UpdateUserInfo?method=showManageUserInfoPage").forward(request, response);
			}
		} else {
			String str_userName = Validate.replaceHtml(request.getParameter("username"));
			String str_userRole = Validate.replaceHtml(request.getParameter("userrole"));
			userId = Validate.replaceHtml(request.getParameter("id"));
			request.setAttribute("userId", userId);
			request.setAttribute("userName", str_userName);
			request.setAttribute("userRole", str_userRole);
			request.getRequestDispatcher("../admin/updateUserInfo.jsp").forward(request, response);
		}
	}
	/**
	 * 修改密码
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public static void updateUserPwd(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String userPwd = Validate.replaceHtml(request.getParameter("newUserPwd"));
		HttpSession session = request.getSession();
		String userName = (String) session.getAttribute("userName");
		if (PublicToolCheckParam.checkNullAndEmpty(userName)
				&& PublicToolCheckParam.checkNullAndEmpty(userPwd)) {
			UserInfoDAO udao = new UserInfoDAO();
			UserInfo ur = udao.getUserId(userName);
			udao.updateUserPwd(ur.getId(), userPwd);
			request.setAttribute("msg", "�޸�����ɹ�");
			request.getRequestDispatcher("../admin/ok.jsp").forward(request,response);
		}
	}

	/**
	 * Constructor of the object.
	 */
	public UpdateUserInfo() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = Validate.replaceHtml(request.getParameter("method"));
		if (PublicToolCheckParam.checkNullAndEmpty(method)) {
			if (method.equals("showUpdatePasswordPage")) {
				showUpdatePasswordPage(request, response);
			} else if (method.equals("showUpdateUserNamedPage")) {
				showUpdateUserNamedPage(request, response);
			} else if (method.equals("updateUserPwd")) {
				updateUserPwd(request, response);
			} else if (method.equals("updateUserName")) {
				updateUserName(request, response);
			} else if (method.equals("checkOldUserPwd")) {
				checkOldUserPwd(request, response);
			} else if (method.equals("showManageUserInfoPage")) {
				showManageUserInfoPage(request, response);
			} else if (method.equals("showAddUserInfoPage")) {
				showAddUserInfoPage(request, response);
			}
			else if (method.equals("insert")) {
				insertInfo(request, response);
			} else if (method.equals("delete")) {
				deleteInfo(request, response);
			} else if (method.equals("edituserInfo")) {
				edituserInfo(request, response);
			}
		}
	}

	public void init() throws ServletException {
	}

}
